package com.my.ecommercesystem.controller;

import com.github.pagehelper.Page;
import com.my.ecommercesystem.dto.MyMessage;
import com.my.ecommercesystem.dto.OrderDto;
import com.my.ecommercesystem.entity.Order;
import com.my.ecommercesystem.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class OrderController {
    @Resource
    private OrderService orderService;

    public OrderService getOrderService() {
        return orderService;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @RequestMapping("/getAllOrder")
    @ResponseBody
    public MyMessage getAllOrder(Integer page, Integer size) {
        Integer mypage = 0;
        Integer mysize = 1;
        MyMessage myMessage = new MyMessage();


        mypage = page;


        mysize = size;
        System.out.println("------------------" + mysize);


        long count = orderService.getUserOrderCount(3);

        int maxpage = (int) (count % mysize == 0 ? (count / mysize) : (count / mysize + 1));

        //上限下限已经由pagehelper修正，不好用，我们自己修正
        mypage = mypage < 1 ? 1 : mypage;
        mypage = mypage > maxpage ? maxpage : mypage;


        Page<OrderDto> list = orderService.queryAllOrder(page, size, 3);

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setOrder_items(orderService.getOrderItem(list.get(i).getOrder_code()));

        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrder_status().equals("待支付")) {
                list.get(i).setOrder_willdo("去支付");
                list.get(i).setOrder_willgo("doPay");

            } else if (list.get(i).getOrder_status().equals("待评价")) {
                list.get(i).setOrder_willdo("去评价");
                list.get(i).setOrder_willgo("还没有评价");
            } else if (list.get(i).getOrder_status().equals("已完成")) {
                list.get(i).setOrder_willdo("申请售后");
                list.get(i).setOrder_willgo("还没有售后");

            }

        }
        myMessage.setData(list);
        myMessage.setPage(page);
        myMessage.setMaxPage(maxpage);
        myMessage.setSize(size);
        myMessage.setCode("200");
        myMessage.setFlag(1);
        myMessage.setInfo("查询成功");


        return myMessage;

    }

    @RequestMapping("/getNumber")
    @ResponseBody
    public long getNumber(Integer a) {
        long count = orderService.getUserOrderCount(a);
        return count;
    }

}
